package com.ykt.mall.ctrl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Model;
import com.ykt.model.Product;
import com.ykt.model.ProductImg;

@ControllerBind(controllerKey="/product")
public class ProductCtrl  extends AdminBaseCtrl<Product>{
	public ProductCtrl() {
		super(Product.class);
	}

	@Override
	protected void otherSave(Model<Product> m) {
		Product.dao.deleteAllPic(m.getInt("id"));//先删除所有
		
		String[] logos = this.getParaValues("logos");
		Date time = new Date();
		int seq=0;
		if(logos==null||logos.length<1) {
			m.set("logo",null);
			return;
		}
		for(String logo : logos){
			Map<String,Object>map = new HashMap<String,Object>();
			map.put("product_id", m.get("id") );
			map.put("url", logo );
			map.put("seq", seq++ );
			map.put("type",0);
			map.put("create_time", time );
			map.put("update_time", time );
			new ProductImg().setAttrs(map).save();
		}
		m.set("logo", logos[logos.length-1]);//最后一张图片设置为商品的默认图片
		m.update();
	}
}
